<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades"></i> 员工信息
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-button
            type="primary"
            icon="el-icon-delete"
            class="handle-del mr10"
            @click="delAllSelection"
        >批量删除</el-button>
        <el-input v-model="query.ename" placeholder="员工名" class="handle-input mr10"></el-input>
        <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
          <el-button type="primary" icon="el-icon-refresh" @click="refresh"></el-button>

      </div>
      <el-table
          :data="tableData"
          border
          class="table"
          ref="multipleTable"
          header-cell-class-name="table-header"
          @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="eid" label="员工编号" width="55" align="center"></el-table-column>
        <el-table-column prop="ename" label="员工名"></el-table-column>
          <el-table-column prop="eusername" label="用户名"></el-table-column>
          <el-table-column prop="sex" label="性别"></el-table-column>
          <el-table-column type='date' prop="hiredate" label="入职日期" width='180'></el-table-column>
          <el-table-column  type='date' prop="leavedate" label="离职日期" width='180'></el-table-column>
          <el-table-column prop="photo" label="头像">
              　<template slot-scope="scope">
              　　　　<img :src="require('../../assets/img/'+scope.row.photo)"  width="40" height="40" class="head_pic"/>
              　　</template>
          </el-table-column>
          <el-table-column prop="tel" label="手机号" width='180'></el-table-column>
          <el-table-column prop="role.rolename" label="角色名"></el-table-column>
          <el-table-column prop="idcard"  label="身份证" width='200'></el-table-column>
          <el-table-column prop="state" label="在职" :formatter='formstate'></el-table-column>
          <el-table-column prop="authstate" label="认证状态" :formatter='formauthstate'></el-table-column>
          <el-table-column label="操作" width="180" align="center">
          <template #default="scope">
            <el-button
                type="text"
                icon="el-icon-edit"
                @click="handleEdit(scope.$index,JSON.parse(JSON.stringify(scope.row)))"
            >编辑
            </el-button>
            <el-button
                type="text"
                icon="el-icon-delete"
                class="red"
                @click="handleDelete(scope.row)"
            >离职
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
<!--        // layout prev 左箭头 pager 页数 next 右箭头
        //@current-change 页数改变时间 有一个参数 就是当前页数
        //total 条数 会根据总条数和每页显示的条数进行 分页-->
        <el-pagination
            background
            layout="prev, pager, next"
            @current-change="preNext"
            :page-size='pagesize'
            :total="total">
        </el-pagination>
      </div>
    </div>
    <!-- 编辑弹出框 -->
    <el-dialog title="编辑" :visible.sync='editVisible' width="30%">
      <el-form ref="form" :model="form" label-width="70px">
        <el-form-item label="员工名" required>
          <el-input v-model="form.ename"></el-input>
        </el-form-item>
          <el-form-item label="用户名">
              <el-input v-model="form.eusername"></el-input>
          </el-form-item>
          <el-form-item label="性别">
              <el-radio-group v-model="form.sex">
                  <el-radio label="男"/>
                  <el-radio label="女"/>
              </el-radio-group>
          </el-form-item>
          <el-form-item label="入职日期">
              <el-input type='datetime' v-model="form.hiredate"></el-input>
          </el-form-item>
          <el-form-item label="离职日期">
              <el-input type='datetime' v-model="form.leavedate"></el-input>
          </el-form-item>
          <el-form-item label="手机号">
              <el-input v-model="form.tel"></el-input>
          </el-form-item>
          <el-form-item :label='placeholder'>
          <template>
              <el-select  v-model="form.roleid" placeholder="请选择" :disabled='disabled'>
                  <el-option
                      v-for="item in role"
                      :label="item.rolename"
                      :value="item.rid">
                  </el-option>
              </el-select>
          </template>
          </el-form-item>
          <el-form-item label="身份证">
              <el-input v-model="form.idcard"></el-input>
          </el-form-item>
      </el-form>
      <template #footer>
                <span class="dialog-footer">
                    <el-button @click="editVisible = false">取 消</el-button>
                    <el-button type="primary" @click="saveEdit">确 定</el-button>
                </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>

export default {
  name: "basetable",
  data() {
    return {
      query: {
        pageNum: 1,
        pageSize: 10
      },
      tableData: [],
      multipleSelection: [],
      delList: [],
      editVisible: false,
      total: 6, pagesize:5,
      form: {},
      idx: -1,
      id: -1,
        role:[],
        imgsrc:[],
        placeholder:'请选择',
        disabled:false
    };
  },
  created() {
    this.getData();
  },
  methods: {
    // 获取 easy-mock 的模拟数据
    getData() {
        //员工列表
     this.$http({
       url:'http://localhost:8090/emp/selectEmp',
       method:'post',
       params:{pageNum:1,pageSize:5,ename:this.ename}
     }).then(response=>{
       //console.log(response)
       this.tableData=response.list
         this.total=response.total
         this.pagesize=response.pageSize
     })
        //获取角色列表
        this.$http({
            url:'http://localhost:8090/emp/selectRole',
            method:'post',
        }).then(res=>{
            this.role=res
        })
    },formstate(row){return row.state===1?'在职':'离职'},formauthstate(row){return row.authstate===2?'未认证':'认证成功'},
    // 触发搜索按钮
    handleSearch() {
      this.$http({
        url:'http://localhost:8090/emp/selectEmp',
        method:'post',
        params:{pageNum:1,pageSize:5,ename:this.query.ename}
      }).then(response=>{
          this.total=response.total
          this.pagesize=response.pageSize
        this.tableData=response.list
      })
    },
    // 离职操作
    handleDelete(row) {
      // 二次确认删除
      this.$confirm("确定要离职该员工吗？", "提示", {
        type: "warning"
      })
          .then(() => {
              let logEusername=sessionStorage.getItem('eusername');
              let thisEusername=row.eusername;
              if(logEusername===thisEusername){
                  this.$message.error('已经登陆不能离职')
              }else {
              this.$http({
                  url:'/emp/empLeave',
                  method:'post',
                  params:{eid:row.eid}
              }).then((res)=>{
                  if(res==true){
                      this.$message.success("离职成功");
                      this.getData()
                  }else {
                      this.$message.error('离职失败');
                  }
              })}

          })
          .catch(() => {});
    },
      // 多选操作
      handleSelectionChange(val) {
          this.multipleSelection = val;
      },
    delAllSelection() {
        let str="";
        for (let i=0;i<this.multipleSelection.length;i++){
            str+=this.multipleSelection[i].eid+","
        }
        console.log(str)
        this.$http({
            url:'/emp/delEmp',
            method:'post',
            params:{str:str}
        }).then(res=>{
            if(res==true){
                this.$message.success('删除成功')
                this.getData()
            }else {
                this.$message.error('删除失败选中有未离职')
            }

        })
    },
    // 编辑操作
    handleEdit(index, row) {
        if(row.authstate==0){
            this.$message.error('未认证不能修改员工角色')
            this.disabled=true
            this.placeholder="未认证"
        }
      this.idx = index;
      this.form = row;
      this.editVisible = true;
    },
    // 保存编辑
    saveEdit() {
      this.$http({
        url:'http://localhost:8090/emp/updEmp',
        method:'post',
        params:{eid:this.form.eid,ename:this.form.ename,eusername:this.form.eusername,sex:this.form.sex,
        hiredate:this.form.hiredate,leavedate:this.form.leavedate,tel:this.form.tel,roleid:this.form.roleid,idcard:this.form.idcard
        }
      }).then(res=>{
        if(res===true){
          this.$message.success("修改成功")
            this.getData()

        }else {
          this.$message.warning("修改失败")
        }
      })
      this.editVisible = false;

    },
    // 分页导航
    handlePageChange() {
      this.getData();
    },preNext(val){
      this.$http({
        url:'http://localhost:8090/emp/selectEmp',
        method:'post',
        params:{pageNum:val,pageSize:5,ename:this.query.ename}
      }).then(response=>{
          this.total=response.total
          this.pagesize=response.pageSize
        this.tableData=response.list
      })

    },refresh(){
        location.reload()
      }
  }
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>
